package com.example.learn_mybatis.service.impl;

import com.example.learn_mybatis.pojo.RoleParam;
import com.example.learn_mybatis.pojo.UserRole;
import com.example.learn_mybatis.mapper.UserRoleMapper;
import com.example.learn_mybatis.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chen
 * @since 2025-07-11
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Override
    public void batchSaveUserRoles(RoleParam roleParam) {
        Integer userId = roleParam.getUserId();
        ArrayList<UserRole> userRoles = new ArrayList<>();
        roleParam.getRoleIds().forEach(roleId -> {
            UserRole userRole = new UserRole();
            userRole.setUserId(Long.valueOf(userId));
            userRole.setRoleId(Long.valueOf(roleId));

            userRoles.add(userRole);
        });

        this.saveBatch(userRoles);
    }

    /**
     * 批量删除
     * @param roleParam
     * @return
     */
    @Override
    public boolean batchRemoveById(RoleParam roleParam) {
        List<Integer> ids = roleParam.getIds();
        return this.removeBatchByIds(ids);
    }
}
